package br.com.sgi.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller("fileUploadBean")
@Scope("session")
public class FileUploadBean {
	
	 private ArrayList<File> files = new ArrayList<File>();
	    private int uploadsAvailable = 1;
	    private boolean autoUpload = false;
	    private boolean useFlash = false;
	    
	    private File fileCarregar;
	    
	    public int getSize() {
	        if (getFiles().size()>0){
	            return getFiles().size();
	        }else 
	        {
	            return 0;
	        }
	    }

	    public FileUploadBean() {
	    }

	    public void paint(OutputStream stream, Object object) throws IOException {
	        stream.write(getFiles().get((Integer)object).getData());
	    }
	    public void listener(UploadEvent event) throws Exception{
	        UploadItem item = event.getUploadItem();
	        File file = new File();
	        file.setLength(item.getData().length);
	        file.setName(item.getFileName());
	        file.setData(item.getData());
	        files.add(file);
	        uploadsAvailable--;
	    }  
	      
	    public String clearUploadData() {
	        files.clear();
	        setUploadsAvailable(1);
	        return null;
	    }
	    
	    public long getTimeStamp(){
	        return System.currentTimeMillis();
	    }
	    
	    public ArrayList<File> getFiles() {
	        return files;
	    }

	    public void setFiles(ArrayList<File> files) { 
	        this.files = files;
	    }

	    public int getUploadsAvailable() {
	        return uploadsAvailable;
	    }
	    
	    public File getFile(){
	    	File file = new File();
	    	if(getFiles() != null && getFiles().size() > 0)	    		
	    		file = getFiles().get((Integer)0);
	    	
	    	return file;
	    }

	    public void setUploadsAvailable(int uploadsAvailable) {
	        this.uploadsAvailable = uploadsAvailable;
	    }

	    public boolean isAutoUpload() {
	        return autoUpload;
	    }

	    public void setAutoUpload(boolean autoUpload) {
	        this.autoUpload = autoUpload;
	    }

	    public boolean isUseFlash() {
	        return useFlash;
	    }

	    public void setUseFlash(boolean useFlash) {
	        this.useFlash = useFlash;
	    }
	   
	    
	    
	    public File getFileCarregar() {
			return fileCarregar;
		}

		public void setFileCarregar(File fileCarregar) {
			if(files == null)
	    		files = new ArrayList<File>();
			if(fileCarregar != null && fileCarregar.getData() != null && files.size() == 0){
				fileCarregar.setLength(fileCarregar.getData().length);
				files.add(fileCarregar);
				uploadsAvailable--;
			}
			
			if(fileCarregar == null || fileCarregar.getData() == null)
				clearUploadData();
			
	    	
			this.fileCarregar = fileCarregar;
		}


}
